package cn.edu.fudan.data;

import cn.edu.fudan.type.BeginEndTime;
import cn.edu.fudan.type.DataItem;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;


public class WriteData {
	
	public <T> void writeData(String FilePath, List<T> data, int num) throws IOException{
		FileWriter fw= new FileWriter(FilePath, false);
		fw.write(" ");
		for(int i = 0; i < data.size(); i ++){
			fw.write(data.get(i).toString()+" ");
		}
		fw.write("\r\n");
		fw.close();
	}
	
	
	public void writeData(String FilePath, List<DataItem> data) throws IOException{
		FileWriter fw= new FileWriter(FilePath, false);
		for(int i = 0; i < data.size(); i ++){
			fw.write(data.get(i).getValue()+"\r\n");
		}
		fw.close();
	}
	
	public void writeDataUCRFormat(String FilePath, List<List<DataItem>> data, int label) throws IOException{
		FileWriter fw= new FileWriter(FilePath, false);
				
		for(List<DataItem> pattern : data){
			fw.write(label+" ");
			for(int i = 0; i < pattern.size(); i ++){
				fw.write(pattern.get(i).getValue()+" ");
			}
			fw.write("\r\n");
		}
				
		fw.close();
	}

	public void writeDataUCRFormat(String FilePath, List<List<DataItem>> data, int label, String splitchar) throws IOException{
		FileWriter fw= new FileWriter(FilePath, false);

		for(List<DataItem> pattern : data){
			fw.write(label+splitchar);
			for(int i = 0; i < pattern.size(); i ++){
				fw.write(pattern.get(i).getValue()+splitchar);
			}
			fw.write("\r\n");
		}

		fw.close();
	}
	
	public void writeDataUCRFormat(String FilePath, List<List<DataItem>> A, List<List<DataItem>> B) throws IOException{
		FileWriter fw= new FileWriter(FilePath, false);
				
		for(List<DataItem> pattern : A){
			fw.write("1 ");
			for(int i = 0; i < pattern.size(); i ++){
				fw.write(pattern.get(i).getValue()+" ");
			}
			fw.write("\r\n");
		}
		for(List<DataItem> pattern : B){
			fw.write("0 ");
			for(int i = 0; i < pattern.size(); i ++){
				fw.write(pattern.get(i).getValue()+" ");
			}
			fw.write("\r\n");
		}
				
		fw.close();
	}

	public void writeDataUCRFormat(String FilePath, List<List<DataItem>> A, List<List<DataItem>> B,String splitchar) throws IOException{
		FileWriter fw= new FileWriter(FilePath, false);

		for(List<DataItem> pattern : A){
			fw.write("1"+splitchar);
			for(int i = 0; i < pattern.size(); i ++){
				fw.write(pattern.get(i).getValue()+splitchar);
			}
			fw.write("\r\n");
		}
		for(List<DataItem> pattern : B){
			fw.write("0"+splitchar);
			for(int i = 0; i < pattern.size(); i ++){
				fw.write(pattern.get(i).getValue()+splitchar);
			}
			fw.write("\r\n");
		}

		fw.close();
	}



	//write every person's event within a time interval
	public void writeDataUCRFormat(String FilePath, List<DataItem> data , int label, BeginEndTime bet) throws IOException {
        FileWriter fw= new FileWriter(FilePath, false);


            fw.write(label+" ");
            for(int i = 0; i < data.size(); i ++){
                if (data.get(i).getTimestamp()>=(bet.getBegin()*10000) && data.get(i).getTimestamp()<=(bet.getEnd()*10000)) {
                    fw.write(data.get(i).getValue() + " ");
                }
            }
            fw.write("\r\n");

        fw.close();


    }
	
	public void writeDataFormat(String FilePath, List<List<DataItem>> A) throws IOException{
		FileWriter fw= new FileWriter(FilePath, false);
				
		int count = 1;
		for(List<DataItem> pattern : A){
			fw.write(count+"||");
			for(int i = 0; i < pattern.size(); i ++){
				fw.write(pattern.get(i).getTimestamp()+" "+pattern.get(i).getValue()+"||");
			}
			fw.write("\r\n");
			count ++;
		}			
		fw.close();
	}
	
	public <T> void writeData(String FilePath, T data, int num) throws IOException{
		FileWriter fw= new FileWriter(FilePath, true);
		fw.write(num+"\t");
		fw.write(data + "\r\n");
		fw.close();
	}
}
